package com.example.controller;


import com.example.Pojo.Emp;
import com.example.Pojo.PageBean;
import com.example.Pojo.Result;
import com.example.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

//员工管理controller层
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    //显示分页列表信息
    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页显示" + page + "," + pageSize + "," + name + "," + gender + "," + begin + "," + end);
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }

    //批量或者单独删除员工
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除了:" + ids);
        empService.deleteById(ids);
        return Result.success();
    }

    //根据id查询员工
    @GetMapping("/emps/{id}")
    public Result selectOne(@PathVariable Integer id) {
        Emp emp = empService.selectOne(id);
        return Result.success(emp);
    }


    //新增员工
    @PostMapping("/emps")
    public Result insert(@RequestBody Emp emp) {
        log.info("新添加了:" + emp);
        empService.insert(emp);
        return Result.success();
    }

    //修改员工
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        empService.updateEmp(emp);
        return Result.success();
    }
}
